﻿using System;
using System.Data.Entity.Infrastructure;
using LM.Negocio;
using LM.Negocio.Objetos;
using LM.Repositorios.Maps;
using System.Data.Entity;

namespace LM.Repositorios
{
    [DbModelBuilderVersion(DbModelBuilderVersion.V5_0)]
    public class Contexto : DbContext
    {
        public Contexto()
            : base("LM")
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists<Contexto>());
            //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Contexto>());
            //Database.SetInitializer(new DropCreateDatabaseAlways<Contexto>());

            //this.Configuration.AutoDetectChangesEnabled = true;
            //this.Configuration.LazyLoadingEnabled = false;
            //this.Configuration.ValidateOnSaveEnabled = true;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new MarcaMap());
            modelBuilder.Configurations.Add(new ClienteMap());
            modelBuilder.Configurations.Add(new VagaMap());
            modelBuilder.Configurations.Add(new TelefoneMap());
            modelBuilder.Configurations.Add(new EnderecoMap());
            modelBuilder.Configurations.Add(new DependenteMap());
            modelBuilder.Configurations.Add(new GaragemMap());
            modelBuilder.Configurations.Add(new TabelaPrecoMap());
            modelBuilder.Configurations.Add(new ItemTabelaPrecoMap());
            modelBuilder.Configurations.Add(new DiaSemanaItemTabelaPrecosMap());
            modelBuilder.Configurations.Add(new ModeloMap());
            modelBuilder.Configurations.Add(new LocacaoMap());
            modelBuilder.Configurations.Add(new OperadorMap());
            modelBuilder.Configurations.Add(new VeiculoMap());
            modelBuilder.Configurations.Add(new PeriodoDoDiaMap());
        }

        public T DesvinculeObjeto<T>(T objeto)
        {
            try
            {
                (this as IObjectContextAdapter).ObjectContext.Detach(objeto);
            }
            catch (Exception)
            {
            }

            return objeto;
        }

        public DbSet<Marca> Marca { get; set; }
        public DbSet<Cliente> Cliente { get; set; }
        public DbSet<Vaga> Vaga { get; set; }
        public DbSet<Telefone> Telefone { get; set; }
		public DbSet<Endereco> Endereco { get; set; }
		public DbSet<Dependente> Dependente { get; set; }
        public DbSet<Garagem> Garagem { get; set; }
        public DbSet<TabelaPreco> TabelaPreco { get; set; }
        public DbSet<ItemTabelaPreco> ItemTabelaPreco { get; set; }
        public DbSet<DiaSemanaItemTabelaPreco> DiaSemanaItemTabelaPreco { get; set; }
        public DbSet<Modelo> Modelo { get; set; }
        public DbSet<Locacao> Locacao { get; set; }
        public DbSet<Operador> Operador { get; set; }
        public DbSet<Veiculo> Veiculo { get; set; }
        public DbSet<PeriodoDoDia> PeriodoDoDia { get; set; }
    }
}
